package beans;

import dao.UsuarioDAO;
import dao.UsuarioDAOHibernate;
import java.awt.event.ActionEvent;
import javax.inject.Named;
import javax.enterprise.context.SessionScoped;
import java.io.Serializable;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpSession;
import modelo.Usuario;
import org.primefaces.context.RequestContext;
import util.AppUtil;

@Named(value = "loginBean")
@SessionScoped
public class loginBean implements Serializable {

    private boolean exibeFuncaoAdm = Boolean.FALSE;
    private boolean exibeFuncaoReserva = Boolean.FALSE;
    private Usuario usuario;
    private UsuarioDAO usuarioDao;

    public loginBean() {
        this.usuarioDao = new UsuarioDAOHibernate();
        this.usuario = new Usuario();
    }

    public boolean isExibeFuncaoAdm() {
        return exibeFuncaoAdm;
    }

    public void setExibeFuncaoAdm(boolean exibeFuncaoAdm) {
        this.exibeFuncaoAdm = exibeFuncaoAdm;
    }

    public boolean isExibeFuncaoReserva() {
        return exibeFuncaoReserva;
    }

    public void setExibeFuncaoReserva(boolean exibeFuncaoReserva) {
        this.exibeFuncaoReserva = exibeFuncaoReserva;
    }

    public Usuario getUsuario() {
        return usuario;
    }

    public void setUsuario(Usuario usuario) {
        this.usuario = usuario;
    }

    public void login(ActionEvent actionEvent) {
        RequestContext context = RequestContext.getCurrentInstance();
        FacesMessage msg;
        boolean loggedIn;
        String raiz = "";
        this.usuario = this.usuarioDao.login(this.usuario);
        if (this.usuario != null) {
            if (this.usuario.getFuncao().getCodFuncao() == 1) {
                this.exibeFuncaoAdm = Boolean.TRUE;
            }
            if (this.usuario.getFuncao().getCodFuncao() == 2) {
                this.exibeFuncaoReserva = Boolean.TRUE;
            }
            loggedIn = true;
            FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("usuario", this.usuario);
            msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Bem vindo", this.usuario.getNomeUsuario());
            raiz = AppUtil.caminhoBaseLogin() + "views/home.xhtml";
        } else {
            loggedIn = false;
            msg = new FacesMessage(FacesMessage.SEVERITY_WARN, "Erro de login", "Usuário e/ou senha incorreto(s).");
            if (this.usuario == null) {
                this.usuario = new Usuario();
            }
        }

        FacesContext.getCurrentInstance().addMessage(null, msg);
        context.addCallbackParam("loggedIn", loggedIn);
        context.addCallbackParam("raiz", raiz);
    }

    public void logout() {
        String raiz = AppUtil.caminhoBaseLogin() + "views/login.xhtml";
        RequestContext context = RequestContext.getCurrentInstance();
        FacesContext facesContext = FacesContext.getCurrentInstance();
        HttpSession sessao = (HttpSession) facesContext.getExternalContext().getSession(false);
        sessao.invalidate();

        context.addCallbackParam("loggedOut", true);
        context.addCallbackParam("raiz", raiz);
    }

}
